﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.Reader.Utils
{
    public static class PageOperateUtils
    {
        /// <summary>
        /// 检查是否登录
        /// </summary>
        /// <param name="callBack">是否登录完成后的回调</param>
        /// <param name="isFoward">登录完成后是否需要导航到下一页面</param>
        //public static void ValidatIsLogin(Action<bool> callBack, bool isFoward = false)
        //{
        //    if (GloabalData.Instance.User == null)
        //    {
        //        new NavidateToLoginPageArgs(callBack, isFoward).NavigateToLogin();
        //    }
        //    else
        //    {
        //        callBack(true);
        //    }
        //}

        private class NavidateToLoginPageArgs
        {
            public Action<bool> CallBack { get; private set; }

            public bool IsFoward { get; private set; }

            public bool IsFromStart { get; private set; }


            Microsoft.Phone.Controls.PhoneApplicationFrame frame;

            public NavidateToLoginPageArgs(Action<bool> callback, bool isfoward)
            {
                this.CallBack = callback;
                this.IsFoward = isfoward;
            }

            public void NavigateToLogin()
            {
                frame = Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame;
                if (frame != null)
                {
                    frame.Navigated += frame_NavidateToLoginPage_Navigated;
                    frame.Navigate(new Uri("/View/UserLoginPage.xaml", UriKind.Relative));
                }
            }

            private void frame_NavidateToLoginPage_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
            {
                if (frame != null)
                {
                    frame.Navigated -= frame_NavidateToLoginPage_Navigated;
                    ILogin login = frame.Content as ILogin;
                    if (login != null)
                    {
                        login.LoginSuccessCallback = this.CallBack;
                        login.IsFoward = this.IsFoward;
                    }
                }
            }
        }
    }
}
